<?php
 
/*
 * Following code will list all the user
 */
 
// array for JSON response
$response = array();
 
// include db connect class
require_once __DIR__ . '/db_connect.php';
 
// connecting to db
$db = new DB_CONNECT();
 
// get all user from user table

$result = mysql_query("SELECT u.*
							,IFNULL( count_rating, 0 ) + IFNULL( count_like, 0 ) + IFNULL( count_comment, 0 ) AS sortcount
						FROM user u
						LEFT JOIN  
							(select user_id_zing, count(id) as count_rating
							 from  rating 
							 group by user_id_zing) as rt
							on rt.user_id_zing = u.user_id_zing
						LEFT JOIN
							(select user_id_zing, count(id) as count_like
							 from  `like` 
							 group by user_id_zing) as lk
							on lk.user_id_zing = u.user_id_zing
						LEFT JOIN
							(select user_id_zing, count(comment_id) as count_comment
							 from  `comment` 
							 group by user_id_zing) as cm
							on cm.user_id_zing = u.user_id_zing
						WHERE u.user_id_zing <> '0'
						ORDER BY sortcount DESC") or die(mysql_error());
 
// check for empty result
if (mysql_num_rows($result) > 0) {
    // looping through all results
    // user node
    $response["user"] = array();
 
    while ($row = mysql_fetch_array($result)) {
        // temp user array
        $user = array();    
	$user["user_id_zing"] = $row['user_id_zing'];
    $user["user_name_zing"] = $row['user_name_zing'];
	$user["user_display_name"] = $row['user_display_name'];
	$user["user_avatar_zing"] = $row['user_avatar_zing'];
	
        // push single product into final response array
        array_push($response["user"], $user);
    }
    // success
    $response["success"] = 1;
 
    // echoing JSON response
    echo json_encode($response["user"]);
} else {
    // no user found
    $response["success"] = 0;
    $response["message"] = "No user found";
 
    // echo no users JSON
    echo json_encode($response);
}
?>